﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Beifenqi
{
    public class 反射文件
    {
        /// <summary>
        /// 为了做成exe的时候不附带一个dll,所以嵌入到exe内
        /// 反射加载dll资源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        /// <returns></returns> 
        //  https://www.cnblogs.com/a849788087/p/6552977.html
        //  例子 AppDomain.CurrentDomain.AssemblyResolve += 反射dll.CurrentDomain_AssemblyResolve;//反射dll
        public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        { 
            foreach (var name in Assembly.GetExecutingAssembly().GetManifestResourceNames())
            {
                var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
                if (stream != null)
                {
                    try
                    {
                        byte[] assemblyData = new byte[stream.Length];
                        stream.Read(assemblyData, 0, assemblyData.Length);
                        return Assembly.Load(assemblyData);
                    }
                    catch (Exception e)
                    {

                    }
                }
            }
            return null;
        } 
    }
}
